cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

capture program drop wagescarze
program define wagescarze
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use final.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
gen poid=sqrt(nbze)

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 dimtm2 ldimm ldimtm2 Y_*  poid, by(ze an)

/* IV */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store `occp'

joinby ze using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\initcaracZE.dta", unm(m)
end

/* non qualified */
wagescarze, occp(bcnq) 

/* second quartile */
gen dimmqt2=dimm*(qtrad==2)
gen dimmqnt2=dimm*(qntrad==2)
gen dimmqc2=dimm*(qconst==2)
gen dimmqp2=dimm*(qlzepop==2)
gen dimmqt275=dimm75*(qtrad==2)
gen dimmqnt275=dimm75*(qntrad==2)
gen dimmqc275=dimm75*(qconst==2)
gen dimmqp275=dimm75*(qlzepop==2)

/* third quartile */
gen dimmqt3=dimm*(qtrad==3)
gen dimmqnt3=dimm*(qntrad==3)
gen dimmqc3=dimm*(qconst==3)
gen dimmqp3=dimm*(qlzepop==3)
gen dimmqt375=dimm75*(qtrad==3)
gen dimmqnt375=dimm75*(qntrad==3)
gen dimmqc375=dimm75*(qconst==3)
gen dimmqp375=dimm75*(qlzepop==3)

/* fourth quartile */
gen dimmqt4=dimm*(qtrad==4)
gen dimmqnt4=dimm*(qntrad==4)
gen dimmqc4=dimm*(qconst==4)
gen dimmqp4=dimm*(qlzepop==4)
gen dimmqt475=dimm75*(qtrad==4)
gen dimmqnt475=dimm75*(qntrad==4)
gen dimmqc475=dimm75*(qconst==4)
gen dimmqp475=dimm75*(qlzepop==4)

ivreg2 drlsnd (dimm = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store v0

ivreg2 drlsnd (dimm dimmqt2 dimmqt3 dimmqt4 = dimm75 dimmqt275 dimmqt375 dimmqt475) Y_* , partial(Y_* ) cl(ze)
estimates store v1

ivreg2 drlsnd (dimm dimmqnt2 dimmqnt3 dimmqnt4 = dimm75 dimmqnt275 dimmqnt375 dimmqnt475) Y_* , partial(Y_* ) cl(ze)
estimates store v2

ivreg2 drlsnd (dimm dimmqc2 dimmqc3 dimmqc4 = dimm75 dimmqc275 dimmqc375 dimmqc475) Y_* , partial(Y_* ) cl(ze)
estimates store v3

ivreg2 drlsnd (dimm dimmqp2 dimmqp3 dimmqp4 = dimm75 dimmqp275 dimmqp375 dimmqp475) Y_* , partial(Y_* ) cl(ze)
estimates store v4

estout v0 v1 v2 v3 v4 , cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 widstat) starlevels(* 0.10 ** 0.05 *** 0.01)

/* IVSELECT */
esttab bcq75 bcqtm282 bcqtm291 bcqtm299 bcqtm207 ///
using "C:\Users\Public\Documents\Tab21\TableA18.rtf" /// 
, append title("lshift")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
